//
//  GeneralParameterManager.m
//  GSBlockChain
//
//  Created by guoshikeji_a on 2018/11/28.
//  Copyright © 2018 guoshikeji. All rights reserved.
//

#import "GeneralParameterManager.h"

//keychain
#import <SAMKeychain.h>

#import "GeneralParameterManager+DeviceModel.h"

@implementation GeneralParameterManager

static GeneralParameterManager *_generalParameterManager;
+ (instancetype)sharedGeneral {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        if (!_generalParameterManager) {
            _generalParameterManager = [[GeneralParameterManager alloc] init];
        }
    });
    return _generalParameterManager;
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        UIDevice *currentDevice = [UIDevice currentDevice];
        //
        self.deviceSystemName = currentDevice.systemName;
        self.deviceSystemVersion = currentDevice.systemVersion;
        self.deviceModel = [self currentDeviceModel];
        
        //设置uuid
        self.uuid = [self getDeviceId];
    }
    return self;
}

- (NSString *)aliasName {
    return [UIDevice currentDevice].name;
}


- (NSString *)appVersion {
    return [[NSBundle mainBundle].infoDictionary objectForKey:@"CFBundleShortVersionString"];
}
- (NSString *)bundleVersion {
    return [[NSBundle mainBundle].infoDictionary objectForKey:@"CFBundleVersion"];
}


#pragma mark -
//获取设备安装后加入keyChain的UUID 卸载后也可以获取
- (NSString *)getDeviceId
{
    NSString *service = SAMKeychainService;
    //用应用bundle ID 作为count
    NSString *acount = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleIdentifier"];
    NSString *currentDeviceUUIDStr = [SAMKeychain passwordForService:service account:acount];
    if (!currentDeviceUUIDStr.length)
    {
        NSUUID * currentDeviceUUID  = [UIDevice currentDevice].identifierForVendor;
        currentDeviceUUIDStr = currentDeviceUUID.UUIDString;
        currentDeviceUUIDStr = [currentDeviceUUIDStr stringByReplacingOccurrencesOfString:@"-" withString:@""];
        currentDeviceUUIDStr = [currentDeviceUUIDStr lowercaseString];
        [SAMKeychain setPassword:currentDeviceUUIDStr forService:service account:acount];
    }
    return currentDeviceUUIDStr;
}


- (NSString *)description
{
    NSString *des = @"\n";
    //
    des = [des stringByAppendingFormat:@"%@=%@\n",NSStringFromSelector(@selector(deviceSystemName)),self.deviceSystemName];
    des = [des stringByAppendingFormat:@"%@=%@\n",NSStringFromSelector(@selector(deviceSystemVersion)),self.deviceSystemVersion];
    des = [des stringByAppendingFormat:@"%@=%@\n",NSStringFromSelector(@selector(deviceModel)),self.deviceModel];
    //
    des = [des stringByAppendingFormat:@"%@=%@\n",NSStringFromSelector(@selector(appVersion)),self.appVersion];
    
    
    return des;
}

@end
